﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Convert.DataAccess;
using System.Configuration;
using Convert.LOTSmodel;
using Convert.CPIAMmodel;
using System.Data.OleDb;
using System.Windows;
namespace Convert.DBconverter
{
    class AgenciesConverter
    {
        private static int BATCH_SAVE_SIZE = 5000;
        public static string LotsConnectionString; //= @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\LOTS\lots.mdb";//ConfigurationManager.ConnectionStrings["LOTStoCPIAMconvert.Properties.Settings.lotsConnectionString"].ConnectionString;
        public static string CpiamConnectionString; //= ConfigurationManager.ConnectionStrings["LOTStoCPIAMconvert.Properties.Settings.CPIAMPOSMaxRetailConnectionString"].ConnectionString;

        public static void ConvertAgencies(string lotsConnectionString, string cpiamConnectionString)
        {
            LotsConnectionString = lotsConnectionString;
            CpiamConnectionString = cpiamConnectionString;
            DataTable AgenciesDT = DataTables.CreateAgentFileDT();
            OleDbConnection dbConn = new OleDbConnection(LotsConnectionString);
            try
            {
                dbConn.Open();
                using (OleDbCommand command = dbConn.CreateCommand())
                {
                    command.CommandText = string.Format(@"SELECT UCASE(AgencyName) AS AgencyName FROM Agency");
                    OleDbDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                            Agency agency = new Agency();
                            int i = 0;
                            agency.AgencyName = reader.IsDBNull(i) ? null : reader.GetString(i);
                            //SaveAccountClass(accountClass);
                            SaveAgency(agency, AgenciesDT);

                        if (AgenciesDT.Rows.Count % BATCH_SAVE_SIZE == 0)
                        {
                            SaveAllDataTables(CpiamConnectionString, AgenciesDT);
                        }
                    }
                    SaveAllDataTables(CpiamConnectionString, AgenciesDT);
                }
            }
            catch (Exception ex)
            {
                Logger.LogWarningAndError("Error in Convert Agencies ", ex);
                MessageBox.Show("Error converting Agencies: " + ex.Message + "\n\nStackTrace=" + ex.StackTrace);
            }
            finally
            {
                dbConn.Close();
            }
        }

        private static void SaveAllDataTables(String ConnectionString, DataTable AgenciesDT)
        {
            if (AgenciesDT.Rows.Count > 0)
                dbSaveBatch.SaveBatch(AgenciesDT, "AgentFile", ConnectionString);
        }

        private static void SaveAgency(Agency agency, DataTable AgentFileDT)
        {
            AgentFile agentFile = new AgentFile(agency.AgencyName);
            AgentFile.SaveAgentFileToDataTable(agentFile, AgentFileDT);
        }
    }
}

